Skip to content

Conversation

@AnSaki57
Copy link

Made modifications to CGStmt.cpp and added a clang CodeGen test named "asm-srcloc-split-literal.c".

…pp and adding a clang CodeGen test named "asm-srcloc-split-literal.c".
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Anakala Sashikiran (AnSaki57)

Changes

Made modifications to CGStmt.cpp and added a clang CodeGen test named "asm-srcloc-split-literal.c".


Full diff: https://github.com/llvm/llvm-project/pull/167316.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGStmt.cpp (+34-9)
  • (added) clang/test/CodeGen/asm-srcloc-split-literal.c (+23)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 36be3295950b8..5a062b74b0607 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2572,24 +2572,49 @@ CodeGenFunction::EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
 static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
                                       CodeGenFunction &CGF) {
   SmallVector<llvm::Metadata *, 8> Locs;
+
+  // We need these to find the correct location for the first line.
+  StringRef StrVal = Str->getString();
+  const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
+  const LangOptions &LangOpts = CGF.CGM.getLangOpts();
+  unsigned StartToken = 0;
+  unsigned ByteOffset = 0;
+
   // Add the location of the first line to the MDNode.
+
+  // Find the offset of the first character that isn't horizontal whitespace.
+  size_t FirstLocOffset = StrVal.find_first_not_of(" \t\v\f");
+
+  // If the string is empty or all-whitespace, default to offset 0.
+  if (FirstLocOffset == StringRef::npos)
+    FirstLocOffset = 0;
+
+  SourceLocation FirstLineLoc = Str->getLocationOfByte(
+    FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+
   Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
-      CGF.Int64Ty, Str->getBeginLoc().getRawEncoding())));
-  StringRef StrVal = Str->getString();
-  if (!StrVal.empty()) {
-    const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
-    const LangOptions &LangOpts = CGF.CGM.getLangOpts();
-    unsigned StartToken = 0;
-    unsigned ByteOffset = 0;
+    CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
 
+  if (!StrVal.empty()) {
     // Add the location of the start of each subsequent line of the asm to the
     // MDNode.
     for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) {
       if (StrVal[i] != '\n') continue;
+
+      // The next line starts at byte offset i + 1.
+      // Find the first non-horizontal-whitespace at or after this offset.
+      size_t NextLineOffset = StrVal.find_first_not_of(" \t\v\f", i + 1);
+
+      // If the rest of the string is empty or all-whitespace,
+      // just use the location right after the newline (i + 1).
+      if (NextLineOffset == StringRef::npos)
+        NextLineOffset = i + 1;
+
       SourceLocation LineLoc = Str->getLocationOfByte(
-          i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+        NextLineOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+
       Locs.push_back(llvm::ConstantAsMetadata::get(
-          llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
+        llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
     }
   }
 
diff --git a/clang/test/CodeGen/asm-srcloc-split-literal.c b/clang/test/CodeGen/asm-srcloc-split-literal.c
new file mode 100644
index 0000000000000..a1d898264e6bd
--- /dev/null
+++ b/clang/test/CodeGen/asm-srcloc-split-literal.c
@@ -0,0 +1,23 @@
+/// Test that inline asm source location corresponds to the actual
+/// instruction line, not the first line of the asm block.
+///
+/// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s
+
+// #include <stdint.h>
+// #include <string.h>
+
+void *memset(void *dest, int c, int n)__attribute__((naked));
+void *memset(void *dest, int c, int n) {
+    __asm__(
+        "\t"            // <-- line with only a tab
+        "xchg %eax, %eax\n" // <-- A valid instruction
+        "\t"            // <-- line with only a tab
+        "mov rdi, 1\n"  // <-- An invalid instruction
+    );
+}
+
+int main() { return 0; }
+
+// CHECK: error: unknown use of instruction mnemonic
+// CHECK-NEXT: mov rdi, 1
+

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions c,cpp -- clang/test/CodeGen/asm-srcloc-split-literal.c clang/lib/CodeGen/CGStmt.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 5a062b74b..94a5c338e 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2590,10 +2590,10 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
     FirstLocOffset = 0;
 
   SourceLocation FirstLineLoc = Str->getLocationOfByte(
-    FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+      FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
 
-  Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
-    CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
+  Locs.push_back(llvm::ConstantAsMetadata::get(
+      llvm::ConstantInt::get(CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
 
   if (!StrVal.empty()) {
     // Add the location of the start of each subsequent line of the asm to the
@@ -2610,11 +2610,12 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
       if (NextLineOffset == StringRef::npos)
         NextLineOffset = i + 1;
 
-      SourceLocation LineLoc = Str->getLocationOfByte(
-        NextLineOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+      SourceLocation LineLoc =
+          Str->getLocationOfByte(NextLineOffset, SM, LangOpts, CGF.getTarget(),
+                                 &StartToken, &ByteOffset);
 
       Locs.push_back(llvm::ConstantAsMetadata::get(
-        llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
+          llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
     }
   }
 

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the title to explain what you're fixing. It's good to put something like "Fixes #164973" somewhere, but not the title/first line.

int main() { return 0; }

// CHECK: error: unknown use of instruction mnemonic
// CHECK-NEXT: mov rdi, 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CHECK lines here don't seem to actually check the line numbers.

/// Test that inline asm source location corresponds to the actual
/// instruction line, not the first line of the asm block.
///
/// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add -o %t or something like that so we don't accidentally get output files in weird places.

Needs // REQUIRES: x86-registered-target somewhere because this uses the x86 asm parser.

/// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s

// #include <stdint.h>
// #include <string.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comments.


// The next line starts at byte offset i + 1.
// Find the first non-horizontal-whitespace at or after this offset.
size_t NextLineOffset = StrVal.find_first_not_of(" \t\v\f", i + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rearrange the loop so you don't have to duplicate the code for the first line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants